package corporationsalary;

import java.util.ArrayList;
import java.util.List;

/**
 * Stupid Algorithm, Good OO.
 * @author Seabook
 *
 */
public class CorporationSalary {
    public static void main(String[] args) {
        CorporationSalary cs = new CorporationSalary();
       
        String[] relations0 = new String[] {"N"};
       
        String[] relations1 = new String[]{"NNNNNN",
                 "YNYNNY",
                 "YNNNNY",
                 "NNNNNN",
                 "YNYNNN",
                 "YNNYNN"};
       
        String[] relations2 = new String[] {"NYNNYN",
                 "NNNNNN",
                 "NNNNNN",
                 "NNYNNN",
                 "NNNNNN",
                 "NNNYYN"};
       
        String[] relations3 = new String[] {"NNNN",
                 "NNNN",
                 "NNNN",
                 "NNNN"};
       
        String[] relations4 = new String[] {"NNYN",
                 "NNYN",
                 "NNNN",
                 "NYYN"};
       
        System.out.println(cs.totalSalary(relations0));
        System.out.println(cs.totalSalary(relations1));
        System.out.println(cs.totalSalary(relations2));
        System.out.println(cs.totalSalary(relations3));
        System.out.println(cs.totalSalary(relations4));
       
    }
   
    public long totalSalary(String[] relations) {
        List<Employee> employees = parseEmployees(relations);

        long sum = 0;
        for (Employee e : employees) {
            sum += e.getSalary();
        }
       
        return sum;
    }

    private List<Employee> parseEmployees(String[] relations) {
       
        List<Employee> employees = new ArrayList<Employee>();
       
        for (int i = 0; i < relations.length; i++) {
            Employee e = new Employee();
            e.employeeNo = i;
            e.relationTable = relations[i];
            employees.add(e);
        }
       
        for (int i = 0; i < employees.size(); i++) {
            Employee e = employees.get(i);
            char[] rels = e.relationTable.toCharArray();
            for (int j = 0; j < rels.length; j++) {
                if (rels[j] == 'Y') {
                    e.addDirSubEmp(employees.get(j));
                }
            }
        }
       
        return employees;
    }
}

class Employee {
    int employeeNo;
    String relationTable;
    List<Employee> directSubEmployees = new ArrayList<Employee>();
   
   
    public long getSalary() {
        if (directSubEmployees.isEmpty()) {
            return 1;
        }
       
        long sum = 0;
        for (Employee employee : directSubEmployees) {
            sum += employee.getSalary();
        }
       
        return sum;
    }
   
    public void addDirSubEmp(Employee emp) {
        directSubEmployees.add(emp);
    }
   
}
